/*
 * Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.svm.core.feature;

import java.util.Map;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;

import com.oracle.svm.core.ParsingReason;
import com.oracle.svm.core.graal.meta.RuntimeConfiguration;
import com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider;
import com.oracle.svm.core.graal.snippets.NodeLoweringProvider;

import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
import jdk.graal.compiler.options.OptionValues;
import jdk.graal.compiler.phases.tiers.Suites;
import jdk.graal.compiler.phases.util.Providers;
import jdk.vm.ci.meta.MetaAccessProvider;
import jdk.vm.ci.meta.ResolvedJavaType;

public interface InternalFeature extends Feature {

    /**
     * Called to register foreign calls.
     *
     * @param foreignCalls The foreign call registry to add to.
     *
     */
    default void registerForeignCalls(SubstrateForeignCallsProvider foreignCalls) {
    }

    /**
     * Called to register Graal invocation plugins.
     *
     * @param providers Providers that the lowering can use.
     * @param plugins Contains the invocation plugins to add to.
     * @param reason The parsing phase that the plugins are registered for
     */
    default void registerInvocationPlugins(Providers providers, Plugins plugins, ParsingReason reason) {
    }

    /**
     * Called to register Graal node plugins.
     *
     * @param providers Providers that the node plugins can use.
     * @param plugins The Plugins object where node plugins can be added to.
     * @param reason The parsing phase that the plugins are registered for
     */
    default void registerGraphBuilderPlugins(Providers providers, Plugins plugins, ParsingReason reason) {
    }

    /**
     * Called to register lowering providers for static analysis, ahead-of-time compilation, and
     * runtime compilation.
     *
     * @param runtimeConfig The runtime configuration.
     * @param options The initial option values.
     * @param providers Providers that the lowering can use.
     * @param lowerings The lowering provider registry to add to.
     * @param hosted True if registering for ahead-of-time compilation, false if registering for
     *            runtime compilation.
     */
    default void registerLowerings(RuntimeConfiguration runtimeConfig, OptionValues options, Providers providers,
                    Map<Class<? extends Node>, NodeLoweringProvider<?>> lowerings, boolean hosted) {
    }

    /**
     * Called to register lowering providers for static analysis, ahead-of-time compilation, and
     * runtime compilation.
     *
     * @param providers Providers that the lowering can use.
     * @param suites The Graal compilation suites to add to.
     * @param hosted True if registering for ahead-of-time compilation, false if registering for
     *            runtime compilation.
     * @param fallback True if registering for fallback compilation, false otherwise.
     */
    default void registerGraalPhases(Providers providers, Suites suites, boolean hosted, boolean fallback) {
    }

    /**
     * Called to register InstalledCodeObserver factories (InstalledCodeObserver.Factory) for
     * runtime compilation.
     *
     * @param runtimeConfig The runtime configuration.
     */
    default void registerCodeObserver(RuntimeConfiguration runtimeConfig) {
    }

    /**
     * Returns true if the feature should be hidden from users.
     *
     * Hidden features are not listed as part of the build output.
     *
     * @since 22.2
     */
    default boolean isHidden() {
        return false;
    }

    @Platforms(Platform.HOSTED_ONLY.class)
    interface AfterAbstractImageCreationAccess extends FeatureAccess {
    }

    /**
     * Handler to add feature specific sections to the image. Note that it is necessary to define
     * symbols in such sections before the normal build process to ensure CGlobals referring to them
     * are able to pick up these symbols.
     *
     * @param access The supported operations that the feature can perform at this time.
     *
     * @since 24.2
     */
    default void afterAbstractImageCreation(AfterAbstractImageCreationAccess access) {
    }

    @Platforms(Platform.HOSTED_ONLY.class)
    interface InternalFeatureAccess extends FeatureAccess {
        ResolvedJavaType findTypeByName(String className);

        MetaAccessProvider getMetaAccess();
    }
}
